package com.gitee.zycra.union.api.dao;

import com.gitee.zycra.jdbc.common.BaseDao;
import com.gitee.zycra.jdbc.common.CommonSelector;
import com.gitee.zycra.union.api.common.model.ApiMockInstance;
import com.gitee.zycra.union.api.common.vo.InstanceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zycra
 */
@Repository
public class ApiMockInstanceDao extends BaseDao<ApiMockInstance> {

    @Autowired
    private CommonSelector commonSelector;

    /**
     * 插入mock实例
     *
     * @param apiMockInstance 实例对象
     * @return 影响行数
     */
    public int insert(ApiMockInstance apiMockInstance) {
        return insertIntoTable(apiMockInstance);
    }

    /**
     * 根据ID更新
     *
     * @param apiMockInstance 实例对象
     * @return 影响行数
     */
    public int updateById(ApiMockInstance apiMockInstance) {
        return updateTableById(apiMockInstance);
    }

    /**
     * 根据ID删除
     *
     * @param id 要删除的ID
     * @return 影响行数
     */
    public int updateToDelete(Long id) {
        return updateTableById(new ApiMockInstance().setId(id).setDeleted(true));
    }

    /**
     * 根据模板ID查询实例
     *
     * @param templateId 模板ID
     * @return 查询结果
     */
    public List<InstanceVO> selectMockInstance(Long templateId) {
        return commonSelector.selectByParam(getJdbcTemplate(),
                "SELECT id, instance_name FROM api_mock_instance WHERE template_id = ? AND deleted = 0",
                InstanceVO.class, List.of(templateId));
    }
}
